//
//  RWNavigationViewController.m
//  baisibudejie
//
//  Created by renwei on 16/9/18.
//  Copyright © 2016年 renwei. All rights reserved.
//

#import "RWNavigationViewController.h"

@interface RWNavigationViewController () <UIGestureRecognizerDelegate>

@end

@implementation RWNavigationViewController

+ (void)load {

    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];

    // 只要通过模型设置，都是通过富文本设置
    // 设置导航条标题 ＝> UINavigationBar
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20.0f];
    [navBar setTitleTextAttributes:attrs];
    
    // 设置导航条北京图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    
    [self.view addGestureRecognizer:pan];
    
    // 控制手势什么时候触发,只有非根控制器才需要触发手势
    pan.delegate = self;
    
    // 禁止之前手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // 假死状态:程序还在运行,但是界面死了.
}

#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    return self.childViewControllers.count > 1;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (self.childViewControllers.count > 0) { // 非根控制器
        
        // 隐藏底部的tabbar
        // 必须要在跳转之前设置
        viewController.hidesBottomBarWhenPushed = YES;

        // 设置返回按钮,只有非根控制器
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithimage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"]  target:self action:@selector(back) title:@"返回"];
    }
    
    // 真正在跳转
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    
    [self popViewControllerAnimated:YES];
}

@end
